package org.ko.core.web.auth;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.ko.core.domain.auth.User;
import org.ko.core.logic.auth.MenuSvc;
import org.ko.core.web.BaseController;
import org.ko.core.websupport.log.Log;
import org.ko.core.websupport.log.impl.LogUitls;
import org.ko.core.websupport.security.encoder.Md5PwdEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 
 * @author zengxm 2014-9-26
 */
@Controller
@RequestMapping("/auth")
public class HomeAct extends BaseController{
	
	@Autowired
	private MenuSvc menuSvc;
	
	/**
	 * 后台登录页面
	 * @return
	 */
	@RequestMapping("login")
	public String login(){
		return view("login");
	}
	
	@Log(message="用户尝试登陆，用户名={0}", params={"name"}, category=Log.USER)
	@RequestMapping("commitLogin")
	public String commitLogin(HttpServletRequest req, User loginUser, ModelMap model){
		/*
		 * 暂时不做验证码
		 */
//		String code = (String) session.getAttribute("validateCode");
//		String submitCode = WebUtils.getCleanParam(request, "validateCode");
//		if (StringUtils.isEmpty(submitCode) || !StringUtils.equals(code,submitCode.toLowerCase())) {
//			return "redirect:/";
//		}
		
		Md5PwdEncoder encoder = new Md5PwdEncoder();
		String password = encoder.encodePassword(loginUser.getPassword());
		UsernamePasswordToken token = null;
		String error = null;
		try {
			Assert.notNull(loginUser, "请输入登录信息");
			Assert.hasText(loginUser.getName(), "登录名不能为空");
			Assert.hasText(loginUser.getPassword(), "密码不能为空");
			
			Subject user = SecurityUtils.getSubject();
			token = new UsernamePasswordToken(
					loginUser.getName(), 
					password
					);
			token.setRememberMe(true);
			
			user.login(token);
			
			LogUitls.putLog("验证通过");
			return redirect("main.do");
		}catch (AuthenticationException e) {
			e.printStackTrace();
			error = "账号或者密码错误";
		}catch(Exception e){
			e.printStackTrace();
			error = e.getMessage();
		}
		
		if(token != null)
			token.clear();
		model.put("error", error);
		model.put("name", loginUser.getName());
		
		LogUitls.putLog(error);
		return view("login");
	}
	
	@Log(message="退出系统", category=Log.USER)
	@RequestMapping(value="logout")
	public String logout(){
		Subject currentUser = SecurityUtils.getSubject();
		currentUser.logout();
		
		return redirect("login.do");
	}
	
	/**
	 * 后台管理主页面
	 * @return
	 */
	@RequestMapping("main")
	public String main(){
		return view("main");
	}
	/**
	 * 后台管理页面左侧
	 * @return
	 */
	@RequestMapping("left")
	public String left(ModelMap model){
		
		try{
			model.addAttribute("menus",menuSvc.findAuthMenus());
		}catch(Exception e){
			e.printStackTrace();
		}
		return view("left");
	}
	
	/**
	 * 后台管理页面右侧
	 * @return
	 */
	@RequestMapping("right")
	public String right(ModelMap model){
		return view("right");
	}
	
	/**
	 * 后台管理页面顶栏
	 * @return
	 */
	@RequestMapping("top")
	public String top(){
		
		
		return view("top");
	}

	@Override
	protected String getTemplatePath() {
		return "core/";
	}
	
}